﻿using System.IO;
using System.Threading;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.DAL;
using Medianamik.Globalization;
using Medianamik.UI.Controls;
using System.Linq;

namespace Medianamik.UI.Web.Shop
{
    public partial class ShopDesjardinsEdit : BaseModuleEditionPage<NodeWrapper>
    {
        protected override void OnInit(System.EventArgs e)
        {
            PublishLinkButton.Visible = CurrentNode.NodeType.IsVersioned;

            RevertButton.Visible = CurrentNode.NodeType.IsVersioned
                                   && IsEdition
                                   && CurrentNode.IsDraft
                                   && !Revision.HasValue
                                   && CurrentNode.DefaultInstance.Revisions.Length > 0;

            RevertButton.Attributes.Add("onclick",
                "return confirm('" +
                Translator.TranslateJS("Medianamik_ContentManagement", "ConfirmRevert") + "');");


            base.OnInit(e);
            Thread.CurrentThread.CurrentCulture = CurrentUICulture;//Sinon en-US
        }

        public override LinkButton SaveButton
        {
            get { return SaveLinkButton; }
        }

        public override MedianamikFormTemplate MedianamikFormTemplate
        {
            get
            {
                return Fields;
            }
        }

        protected override string TypeIdParamName
        {
            get
            {
                return DefaultParamNames.TypeID;
            }
        }

        protected override string ParentNodeIdParamName
        {
            get
            {
                return DefaultParamNames.ParentNodeID;
            }
        }

        //protected override void Save(object sender, System.EventArgs e)
        //{
        //    Page.Validate(BaseMedianamikControl.DefaultValidationGroupName);

        //    var node = MedianamikFormTemplate.GetFormValue(CurrentNode);

        //    var xmlReceiptTemplate = node.Instances[0].Properties.FirstOrDefault(p => p.Name
        //        .Equals(Data.Desjardins.Columns.ReceiptTemplate)).Value.ToString();
        //    var xmlPaymentTemplate = node.Instances[0].Properties.FirstOrDefault(p => p.Name
        //        .Equals(Data.Desjardins.Columns.PaymentTemplate)).Value.ToString();
        //    var xmlCSSClasses = node.Instances[0].Properties.FirstOrDefault(p => p.Name
        //        .Equals(Data.Desjardins.Columns.CSSClasses)).Value.ToString();
        //    var merchantKey = node.Instances[0].Properties.FirstOrDefault(p => p.Name
        //        .Equals(Data.Desjardins.Columns.MerchantKey)).Value.ToString();
        //    var url = node.Instances[0].Properties.FirstOrDefault(p => p.Name
        //        .Equals(Data.Desjardins.Columns.PaymentProviderUrl)).Value.ToString();

        //    SendTemplate(merchantKey, url, xmlPaymentTemplate);
        //    SendTemplate(merchantKey, url, xmlReceiptTemplate);
        //    SendCSSClasses(merchantKey, url, xmlCSSClasses);

        //    //base.Save(sender, e);
        //}



        private NodeWrapper _newNode;
        protected NodeWrapper NewNode
        {
            get { return _newNode ?? (_newNode = MedianamikFormTemplate.GetFormValue(CurrentNode)); }

        }

        protected void SendCSSClasses(object sender, System.EventArgs e)
        {
            if (!string.IsNullOrEmpty(CSSClasses))
            {
                var result = Desjardins.Transaction.Customization.CSSClasses.SendCSSClasses(MerchantKey, PaymentProviderUrl, CSSClasses);

                if (string.IsNullOrEmpty(result.ErrorCode))
                {
                    MessageLabel.Text = "Success";
                }
                else
                {
                    MessageLabel.Text = string.Format("Error: {0}<br/>{1}", result.ErrorCode, result.ServerResponse);
                }
            }
            else
            {
                MessageLabel.Text = "Xml is empty";
            }
        }

        protected void SendTemplate(string merchantKey, string url, string xml)
        {
            if(!string.IsNullOrEmpty(xml))
            {
                var result = Desjardins.Transaction.Customization.Template.SendTemplate(merchantKey, url, xml);

                if (string.IsNullOrEmpty(result.ErrorCode))
                {
                    MessageLabel.Text = "Success";
                }
                else
                {
                    MessageLabel.Text = string.Format("Error: {0}<br/>{1}", result.ErrorCode, result.ServerResponse);
                }
            }
            else
            {
                MessageLabel.Text = "Xml is empty";
            }
        }

        protected void SendPaymentTemplate(object sender, System.EventArgs e)
        {
            SendTemplate(MerchantKey, PaymentProviderUrl, PaymentTemplate);
        }

        protected void SendReceiptTemplate(object sender, System.EventArgs e)
        {
            SendTemplate(MerchantKey, PaymentProviderUrl, ReceiptTemplate);
        }

        private string _merchantKey;
        protected string MerchantKey
        {
            get
            {
                return _merchantKey ?? (_merchantKey = NewNode.Instances[0].Properties.FirstOrDefault(p => p.Name
                .Equals(Data.Desjardins.Columns.MerchantKey)).Value.ToString());
            }
        }


        private string _paymentProviderUrl;
        protected string PaymentProviderUrl
        {
            get
            {
                return _paymentProviderUrl ?? (_paymentProviderUrl = NewNode.Instances[0].Properties.FirstOrDefault(p => p.Name
                .Equals(Data.Desjardins.Columns.PaymentProviderUrl)).Value.ToString());
            }
        }

        private string _paymentTemplate;
        protected string PaymentTemplate
        {
            get
            {
                return _paymentTemplate ?? (_paymentTemplate = NewNode.Instances[0].Properties.FirstOrDefault(p => p.Name
                .Equals(Data.Desjardins.Columns.PaymentTemplate)).Value.ToString());
            }
        }

        private string _receiptTemplate;
        protected string ReceiptTemplate
        {
            get
            {
                return _receiptTemplate ?? (_receiptTemplate = NewNode.Instances[0].Properties.FirstOrDefault(p => p.Name
                .Equals(Data.Desjardins.Columns.ReceiptTemplate)).Value.ToString());
            }
        }

        private string _cssClasses;
        protected string CSSClasses
        {
            get
            {
                return _cssClasses ?? (_cssClasses = NewNode.Instances[0].Properties.FirstOrDefault(p => p.Name
                .Equals(Data.Desjardins.Columns.CSSClasses)).Value.ToString());
            }
        }
    }
}
